using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using System.IO.Ports;
using System.Text;
using SecretLabs.NETMF.Diagnostics;

#if MINI
using SecretLabs.NETMF.Hardware.NetduinoMini;
#else
using SecretLabs.NETMF.Hardware.Netduino;
#endif

namespace codebase
{
    public class windowSettings : UI.Window
    {
        UI.Frame serialFrame;
        UI.Frame optionsFrame;

        UI.Label lBaud;
        public UI.RadioButton setBaud;
        UI.Label lParity;
        public UI.RadioButton setParity;
        UI.Label lDataBits;
        public UI.RadioButton setDataBits;
        UI.Label lStopBits;
        public UI.RadioButton setStopBits;
        UI.Button setBack;
        public UI.CheckBox useAnswerLength;
        public UI.Button setAnswerLength;
        public UI.CheckBox useTimeOut;
        public UI.Button setTimeOut;
        UI.Button setDebug;

        SerialMonitor mon;

        public windowSettings(UI.WindowManager manager, SerialMonitor monitor)
        {
            Initialize(manager, monitor);
        }

        public void Initialize(UI.WindowManager manager, SerialMonitor monitor)
        {
            base.Initialize(manager, 0, 0);

            mon = monitor;

            FullScreen = true;
            Text = "Settings";
            Gradient = true;

            serialFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 1, 2);
            serialFrame.Width = cX(Parent.screen.Width / Parent.screen.CharacterWidth - 2);
            serialFrame.Height = cY(16);
            serialFrame.Text = "Port Settings";

            optionsFrame = (UI.Frame)Add(UI.Window.ControlTypes.Frame, 1, 19);
            optionsFrame.Width = cX(Parent.screen.Width / Parent.screen.CharacterWidth - 2);
            optionsFrame.Height = cY(20);
            optionsFrame.Text = "General Settings";

            lBaud = (UI.Label)Add(UI.Window.ControlTypes.Label, 1, 3);
            lBaud.Text = "Baud Rate";
            lBaud.BackgroundColor = ColorExtensions.RGB16(220, 220, 220);
            setBaud = (UI.RadioButton)Add(UI.Window.ControlTypes.RadioButton, 1, 4);
            setBaud.updateLabels(new string[14] { "110", "300", "600", "1200", "2400", "4800", "9600", "14400", "19200", "31250", "38400", "56000", "57600", "115200" });
            setBaud.Index = mon.settings.bIndex;
            setBaud.SelectedIndexChanged += new UI.RadioButton.SelectedIndexChangedDelegate(setBaud_SelectedIndexChanged);

            lParity = (UI.Label)Add(UI.Window.ControlTypes.Label, 12, 3);
            lParity.Text = "Parity";
            lParity.BackgroundColor = ColorExtensions.RGB16(220, 220, 220);
            setParity = (UI.RadioButton)Add(UI.Window.ControlTypes.RadioButton, 12, 4);
            setParity.updateLabels(new string[5] { "Even", "Mark", "None", "Odd", "Space" });
            setParity.Index = mon.settings.pIndex;
            setParity.SelectedIndexChanged += new UI.RadioButton.SelectedIndexChangedDelegate(setParity_SelectedIndexChanged);

            lDataBits = (UI.Label)Add(UI.Window.ControlTypes.Label, 20, 3);
            lDataBits.Text = "Data Bits";
            lDataBits.BackgroundColor = ColorExtensions.RGB16(220, 220, 220);
            setDataBits = (UI.RadioButton)Add(UI.Window.ControlTypes.RadioButton, 20, 4);
            setDataBits.updateLabels(new string[2] { "7", "8" });
            setDataBits.Index = mon.settings.dIndex;
            setDataBits.SelectedIndexChanged += new UI.RadioButton.SelectedIndexChangedDelegate(setDataBits_SelectedIndexChanged);

            lStopBits = (UI.Label)Add(UI.Window.ControlTypes.Label, 20, 7);
            lStopBits.Text = "Stop Bits";
            lStopBits.BackgroundColor = ColorExtensions.RGB16(220, 220, 220);
            setStopBits = (UI.RadioButton)Add(UI.Window.ControlTypes.RadioButton, 20, 8);
            setStopBits.updateLabels(new string[4] { "None", "One", "OnePointFive", "Two" });
            setStopBits.Index = mon.settings.sIndex;
            setStopBits.SelectedIndexChanged += new UI.RadioButton.SelectedIndexChangedDelegate(setStopBits_SelectedIndexChanged);

            useAnswerLength = (UI.CheckBox)Add(UI.Window.ControlTypes.CheckBox, 1, 20);
            useAnswerLength.Text = "Use fixed Length";
            useAnswerLength.IsChecked = mon.settings.waitForAnswer;
            useAnswerLength.CheckStateChanged += new UI.CheckBox.CheckStateChangedDelegate(useAnswerLength_CheckStateChanged);

            setAnswerLength = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 21);
            setAnswerLength.Text = "\n Set Answer Length    \n";
            setAnswerLength.Released += new UI.Button.ButtonReleasedDelegate(setAnswerLength_Released);

            useTimeOut = (UI.CheckBox)Add(UI.Window.ControlTypes.CheckBox, 1, 25);
            useTimeOut.Text = "Use Timeout";
            useTimeOut.IsChecked = mon.settings.waitForTimeOut;
            useTimeOut.CheckStateChanged += new UI.CheckBox.CheckStateChangedDelegate(useTimeOut_CheckStateChanged);

            setTimeOut = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 26);
            setTimeOut.Text = "\n Set Timeout Interval \n";
            setTimeOut.Released += new UI.Button.ButtonReleasedDelegate(setTimeOut_Released);

            setDebug = (UI.Button)Add(UI.Window.ControlTypes.Button, 20, 36);
            getDebugText();
            setDebug.Released += new UI.Button.ButtonReleasedDelegate(setDebug_Released);

            setBack = (UI.Button)Add(UI.Window.ControlTypes.Button, 1, 36);
            setBack.Text = "\n Back \n";
            setBack.Released += new UI.Button.ButtonReleasedDelegate(back_Pressed);
        }

        void setStopBits_SelectedIndexChanged(int idx)
        {
            mon.settings.StopBits = setStopBits.Items[idx];
            mon.settings.sIndex = idx;
        }

        void setDataBits_SelectedIndexChanged(int idx)
        {
            mon.settings.DataBits = setDataBits.Items[idx];
            mon.settings.dIndex = idx;
        }

        void setParity_SelectedIndexChanged(int idx)
        {
            mon.settings.Parity = setParity.Items[idx];
            mon.settings.pIndex = idx;
        }

        void setBaud_SelectedIndexChanged(int idx)
        {
            mon.settings.BaudRate = setBaud.Items[idx];
            mon.settings.bIndex = idx;
        }

        void setAnswerLength_Released(UI.Button btn)
        {
            windowInput input = new windowInput(Parent, mon);
            input.Show(Settings.Inputs.SetAnswerLength, mon.settings.answerLength.ToString(), false);
        }

        void setTimeOut_Released(UI.Button btn)
        {
            windowInput input = new windowInput(Parent, mon);
            input.Show(Settings.Inputs.SetTimeOutInterval, mon.settings.timeOutInterval.ToString(), false);
        }

        void back_Pressed(UI.Button btn)
        {
            mon.formMain.Visible = true;

            Parent.Remove(this);

            Parent.Redraw();
        }

        void getDebugText()
        {
            switch (Transport.GetInterface())
            {
                case TransportInterface.Com2:
                    setDebug.Text = "\n Debug: COM2 \n";
                    break;
                case TransportInterface.Com1:
                    setDebug.Text = "\n Debug: COM1 \n";
                    break;
                case TransportInterface.Usb1:
                    setDebug.Text = "\n Debug: USB1 \n";
                    break;
                case TransportInterface.None:
                    setDebug.Text = "\n Debug: None \n";
                    break;
                default:
                    setDebug.Text = "\n Debug: Error \n";
                    break;
            }
            if (Visible) setDebug.Refresh();
        }

        void setDebug_Released(UI.Button btn)
        {
            switch (Transport.GetInterface())
            {
                case TransportInterface.Com2:
                    Transport.SetInterface(TransportInterface.None);
                    break;
                default:
                    Transport.SetInterface(TransportInterface.Com2);
                    break;
            }

            getDebugText();
        }

        void useAnswerLength_CheckStateChanged(bool check)
        {
            mon.settings.waitForAnswer = check;
        }

        void useTimeOut_CheckStateChanged(bool check)
        {
            mon.settings.waitForTimeOut = check;
        }
    }
}
